package com.btshop.goods.service.impl;
import com.btshop.goods.mapper.AlbumMapper;
import com.btshop.goods.pojo.Album;
import com.btshop.goods.pojo.AlbumExample;
import com.btshop.goods.service.AlbumService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import com.util.IdWorker;

/**
 * @ClassName: AlbumServiceImpl
 * @description: Album业务层接口实现类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Service
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private IdWorker idWorker;


    /**
     * Album条件+分页查询
     * @param album 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Album> findPage(Album album, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        AlbumExample example = createExample(album);
        //执行搜索
        return new PageInfo<Album>(albumMapper.selectByExample(example));
    }

    /**
     * Album分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Album> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Album>(this.buildSelectAll());
    }

    /**
     * Album条件查询
     * @param album
     * @return
     */
    @Override
    public List<Album> findList(Album album){
        //构建查询条件
        AlbumExample example = createExample(album);
        //根据构建的条件查询数据
        return albumMapper.selectByExample(example);
    }


    /**
     * Album构建查询对象
     * @param album
     * @return
     */
    public AlbumExample createExample(Album album){
        AlbumExample example=new AlbumExample();
        AlbumExample.Criteria criteria = example.createCriteria();
        if(album!=null){
            // 主键ID
            if(!StringUtils.isEmpty(album.getId())){
                    criteria.andIdEqualTo(album.getId());
            }
            // 相册名称
            if(!StringUtils.isEmpty(album.getTitle())){
                    criteria.andTitleLike("%"+album.getTitle()+"%");
            }
            // 相册封面
            if(!StringUtils.isEmpty(album.getImage())){
                    criteria.andImageEqualTo(album.getImage());
            }
            // 创建时间
            if(!StringUtils.isEmpty(album.getCreateTime())){
                    criteria.andCreateTimeEqualTo(album.getCreateTime());
            }
            // 更新时间
            if(!StringUtils.isEmpty(album.getUpdateTime())){
                    criteria.andUpdateTimeEqualTo(album.getUpdateTime());
            }
            // 乐观锁版本号
            if(!StringUtils.isEmpty(album.getOptimistic())){
                    criteria.andOptimisticEqualTo(album.getOptimistic());
            }
            // 备注字段
            if(!StringUtils.isEmpty(album.getRemark())){
                    criteria.andRemarkEqualTo(album.getRemark());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delete(Long id){
        albumMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Album
     * @param album
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void update(Album album){
        albumMapper.updateByPrimaryKey(album);
    }

    /**
     * 增加Album
     * @param album
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void add(Album album){
        //设置主键值
        album.setId(idWorker.nextId());
        albumMapper.insert(album);
    }

    /**
     * 根据ID查询Album
     * @param id
     * @return
     */
    @Override
    public Album findById(Long id){
        return  albumMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Album全部数据
     * @return
     */
    @Override
    public List<Album> findAll() {
        return this.buildSelectAll();
    }

    public List<Album> buildSelectAll(){
        return albumMapper.selectByExample(null);
    }
}
